package com.xe.gae.control;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.channels.Channels;
import java.util.HashMap;
import java.util.Map;

import com.google.appengine.api.blobstore.BlobInfo;
import com.google.appengine.api.blobstore.BlobInfoFactory;
import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreInputStream;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Text;
import com.google.appengine.api.files.AppEngineFile;
import com.google.appengine.api.files.FileReadChannel;
import com.google.appengine.api.files.FileService;
import com.google.appengine.api.files.FileServiceFactory;
import com.xe.gae.model.XEResource;
import com.xe.gae.model.XETree;
import com.xe.util.XUtil;

public class XEResourcesManager {
	
	static String FOLDER="folder";
	
	
	XEDataStorageService dss = new XEDataStorageService();

	public String getEditor(XEResource r){
		XUtil u = new XUtil();
		String e=u.getEditor(r.getPath());
		return e;
	}
	public String getEditorMime(XEResource r){
		XUtil u = new XUtil();
		String e=u.getEditorMime(r.getPath());
		return e;
	}
	public String getEncodeKey(XEResource r){
		if (r==null) return null;
		String s=  KeyFactory.keyToString(r.getKey());
		return s;
	}
	
	public XEResource findByEncodedKey(String encoded){
		XEResource r = (XEResource) dss.findByEncodeKey(XEResource.class, encoded);
		return r;
		
	}

	
	public XEResource findByKey(String skey){
		XEResource r = (XEResource) dss.findByKey(XEResource.class, skey);
		return r;
	}
	
	public XEResource getRoot(XETree tree){
		XEResource root= (XEResource) dss.findByKey(XEResource.class , "/");
		
		if (root==null){ 
			root=new XEResource();
			root.setTree(tree);
			root.setPath("/");
			root.setIsFolder(true);
			root=(XEResource) dss.put(root);
		}
		
		return root;
	}
	
	
	public XEResource getRoot(){
		XEResource root= (XEResource) dss.findByKey(XEResource.class , "/");
		
		if (root==null){ 
			XETree tree = (XETree) dss.findByKey(XETree.class, "_XE");
			root=new XEResource();
			root.setTree(tree);
			root.setPath("/");
			root.setIsFolder(true);
			root=(XEResource) dss.put(root);
		}
		
		return root;
	}
	
	public XEResource newFolder(XEResource parent,String name)
	{
		if (parent==null) parent = getRoot();
		XEResource resource = new XEResource();
		
		resource.setParent(parent);
		resource.setTree(parent.getTree());
		
		resource.setPath(parent.getPath()+name+ "/");	
		
		
		resource.setIsFolder(true);
		
		dss.beginTransaction();
		resource=(XEResource) dss.put(resource);
		dss.commit();
		return resource;
	}
	
	
	
	public XEResource newResource(BlobKey blobKey, XEResource parent) {
		XEResource res = new XEResource();
		BlobInfoFactory bif = new BlobInfoFactory();
		BlobInfo info = bif.loadBlobInfo(blobKey);
		String filename=info.getFilename();
		res.setPath(parent.getPath()+filename);
		res.setTree(parent.getTree());
		res.setParent(parent);
		res.setIsFolder(false);
		res.setBlobKey(blobKey);
		XUtil utl=new XUtil();
		String ext=utl.getFileExt(filename);
		String editor=utl.getEditor(filename);
		res.setMimeType(utl.getMimeTypeByFileName(filename));
		String line="";
			try {
				if (editor!=null)
					{
					
					FileService fileService = FileServiceFactory.getFileService();
					AppEngineFile file = fileService.getBlobFile(blobKey);
					FileReadChannel readChannel = fileService.openReadChannel(file, false);
					BufferedReader reader = new BufferedReader(Channels.newReader(readChannel, "UTF8"));
				  
				  line += reader.readLine();
				  while (reader.ready()) 
					  {line += reader.readLine() + System.getProperty("line.separator"); ;
				  }
					res.setSource(new Text(line));
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		dss.beginTransaction();
		res = (XEResource) dss.put(res);
		dss.commit();
		return res;
		
	}

	public String getFileExtension(String name){
		String[] temp = name.split("\\.");
		int x = temp.length;
		String ext=temp[x-1];
		return ext;
	}
	

}
